#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def article_analysis():
    ht = {}
    for i in xrange(1, 368):
        infile = open("type/article_types_" + str(i) + ".txt", "r")
        for line in infile:
            line = line.rstrip()
            splitline = line.split("\t")
            name = splitline[0]
            count = int(splitline[1])
            if ht.has_key(name):
                ht[name] += count
            else:
                ht[name] = count
        infile.close()

    outfile = open("all_article_types.txt", "w")
    for item in ht.keys():
        outfile.write(item + "\t" + str(ht[item]) + "\n")
        outfile.flush()
    outfile.close()


def journal_analysis():
    ht = {}
    for i in xrange(1, 368):
        infile = open("type/journal_types_" + str(i) + ".txt", "r")
        for line in infile:
            line = line.rstrip()
            splitline = line.split("####")
            name = splitline[0]
            if not ht.has_key(name):
                ht[name] = {}
            types = splitline[1]
            for item in types.split("|"):
                splititem = item.split("=")
                if len(splititem) == 2:
                    type = splititem[0]
                    type_count = int(splititem[1])
                    if ht[name].has_key(type):
                        ht[name][type] += type_count
                    else:
                        ht[name][type] = type_count
                else:
                    print "Error line: " + line
                    print "Error item: " + item
        infile.close()
    outfile = open("all_journal_types.txt", "w")
    outfile2 = open("all_journal_types.txt.stat", "w")
    max_journal_name = ""
    max_journal_count = 0
    min_journal_name = ""
    min_journal_count = 5000000
    total_type_count = 0
    total_article_count = 0
    for item in ht.keys():
        out_line = ""
        journal_count = 0
        types = len(ht[item])
        total_type_count += types
        max_type_name = ""
        max_type_count = 0
        min_type_name = ""
        min_type_count = 5000000
        for item2 in ht[item].keys():
            type_count = ht[item][item2]
            journal_count += type_count
            out_line += item2 + "=" + str(type_count) + "|"
            if type_count > max_type_count:
                max_type_name = item2
                max_type_count = type_count
            if type_count < min_type_count:
                min_type_name = item2
                min_type_count = type_count
        average_type_count = float(journal_count) / float(types)
        out_line = out_line[:-1]
        outfile.write(item + "####" + str(journal_count) + "####" + str(
            average_type_count) + "####" + max_type_name + "####" + str(max_type_count) + "####" + min_type_name + "####" + str(min_type_count) + "####" + out_line + "\n")
        outfile.flush()
        if types > max_journal_count:
            max_journal_name = item
            max_journal_count = types
        if types < min_journal_count:
            min_journal_name = item
            min_journal_count = types
        total_article_count += journal_count
    outfile.close()
    count = len(ht)
    average_types_count = float(total_type_count) / float(count)
    outfile2.write("Total Article Count" + str(total_article_count) + "\n")
    outfile2.write("Total Journal Count" + str(count) + "\n")
    outfile2.write("Max Types Count Journal: " + max_journal_name + "=" + str(max_journal_count) + "\n")
    outfile2.write("Min Types Count Journal: " + min_journal_name + "=" + str(min_journal_count) + "\n")
    outfile2.write("Average Types Count: " + str(average_types_count) + "\n")
    outfile2.flush()
    outfile2.close()


if __name__ == "__main__":
    journal_analysis()